﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SmartSiteManagementSystem.Appweb.Common;
using SmartSiteManagementSystem.Appweb.Models;
using SmartSiteManagementSystem.IServices;
using SmartSiteManagementSystem.Models.User;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SmartSiteManagementSystem.Appweb.Controllers
{
    /// <summary>
    /// https://echarts.apache.org/examples/zh/editor.html?c=dynamic-data
    /// </summary>
    public class HealthMonitorController : Controller
    {
        private readonly ILogger<HealthMonitorController> _logger;
        private readonly IUserService _userService;
        public HealthMonitorController(IUserService userService, ILogger<HealthMonitorController> logger)
        {
            _userService = userService;
            _logger = logger;
        }

       

        public IActionResult Index(string param)
        {
            string userName = "";

    
            IList<string> outputArrXStrList = new List<string>();
            IList<int> outputArrYIntList = new List<int>();


            IList<string> outputTArrXStrList2 = new List<string>();
            IList<int> outputTArrYIntList2 = new List<int>();
            string paramStr = System.Web.HttpUtility.UrlDecode(param);
            IList<UserHealthMonitorInfo> hmlist = new List<UserHealthMonitorInfo>();
            try
            {
                if (!string.IsNullOrEmpty(paramStr))
                {
                    AppBasicParameters info = paramStr.JsonToObject<AppBasicParameters>();
                    if(info!=null)
                    {
                        UserInfo userInfo = _userService.GetUserInfoById(Convert.ToInt32(info.AppUserId));
                        if (userInfo != null)
                        {
                            userName = userInfo.ShowName;
                            
                            Hashtable uparam = new Hashtable();
                            DateTime nowtime = DateTime.Now;
                            DateTime beforttime = DateTime.Now.AddHours(-1);
                            uparam.Add("user_id", info.AppUserId);
                            uparam.Add("start_time", beforttime.ToString("yyyy-MM-dd HH:mm:ss"));

                            uparam.Add("end_time", nowtime.ToString("yyyy-MM-dd HH:mm:ss"));
    

                            hmlist = _userService.GetHealthMonitorList(uparam, "", 0, 100);
                            if (hmlist.Count > 0)
                            {
                                foreach (UserHealthMonitorInfo item in hmlist)
                                {
                                    outputArrXStrList.Add(item.CollectTime.Hour.ToString() + ":" + item.CollectTime.Minute.ToString() + ":" + item.CollectTime.Second.ToString());

                                             outputTArrXStrList2.Add(item.CollectTime.Hour.ToString() + ":" + item.CollectTime.Minute.ToString() + ":" + item.CollectTime.Second.ToString());


                                    outputArrYIntList.Add(Convert.ToInt32(item.HeartBeat.ToString()));

                                    outputTArrYIntList2.Add(Convert.ToInt32(item.Temperature.ToString()));
                                }
                            }
 
                        }
                    }

                }

            }
            catch(Exception e)
            {
                _logger.LogError(e.Message.ToString());
            }
            if (outputArrXStrList.Count == 0)
            {
                outputArrXStrList = new List<string>();

                outputArrXStrList.Add(DateTime.Now.AddHours(-1).Hour.ToString() + ":" + DateTime.Now.AddHours(-1).Minute.ToString() + ":" + DateTime.Now.AddHours(-1).Second.ToString());
                outputArrXStrList.Add(DateTime.Now.AddMinutes(-30).Hour.ToString() + ":" + DateTime.Now.AddMinutes(-30).Minute.ToString() + ":" + DateTime.Now.AddMinutes(-30).Second.ToString());

                outputArrXStrList.Add(DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString());
            }
            if(outputTArrXStrList2.Count==0)
            {
                outputTArrXStrList2 = new List<string>();

                outputTArrXStrList2.Add(DateTime.Now.AddHours(-1).Hour.ToString() + ":" + DateTime.Now.AddHours(-1).Minute.ToString() + ":" + DateTime.Now.AddHours(-1).Second.ToString());
                outputTArrXStrList2.Add(DateTime.Now.AddMinutes(-30).Hour.ToString() + ":" + DateTime.Now.AddMinutes(-30).Minute.ToString() + ":" + DateTime.Now.AddMinutes(-30).Second.ToString());

                outputTArrXStrList2.Add(DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString());

            }



            if(outputArrYIntList.Count==0)
            {
                outputArrYIntList = new List<int>();
                outputArrYIntList.Add(0);
                outputArrYIntList.Add(0); outputArrYIntList.Add(0);
            }

            if (outputTArrYIntList2.Count == 0)
            {
                outputTArrYIntList2 = new List<int>();
                outputTArrYIntList2.Add(0);
                outputTArrYIntList2.Add(0); outputTArrYIntList2.Add(0);
            }


            ViewBag.arrXStrList = outputArrXStrList;
            ViewBag.arrYIntList = outputArrYIntList;
            ViewBag.arrXStrList2 = outputTArrXStrList2;

            ViewBag.arrYIntList2 = outputTArrYIntList2;

            ViewBag.userName = userName;

            return View();
        }
    }
}
